package com.trinity.specialpay.service.impl;

import com.trinity.common.core.domain.entity.SysDept;
import com.trinity.common.utils.StringUtils;
import com.trinity.orders.domain.XcOrders;
import com.trinity.orders.domain.XcPayRecord;
import com.trinity.orders.service.IXcOrdersService;
import com.trinity.orders.service.IXcPayRecordService;
import com.trinity.specialpay.domain.CampusActivityStudent;
import com.trinity.specialpay.dto.QueryActivityParamsDto;
import com.trinity.specialpay.dto.QueryActivityStudentsRequestDto;
import com.trinity.specialpay.mapper.CampusActivityStudentMapper;
import com.trinity.specialpay.service.ICampusActivityStudentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 缴费名单Service业务层处理
 *
 * @author trinity
 * @date 2023-11-21
 */
@Service
public class CampusActivityStudentServiceImpl implements ICampusActivityStudentService {
    @Resource
    private CampusActivityStudentMapper campusActivityStudentMapper;


    @Resource
    private IXcOrdersService ordersService;

    @Resource
    private IXcPayRecordService payRecordService;


    /**
     * 查询缴费名单
     *
     * @param payId 缴费名单主键
     * @return 缴费名单
     */
    @Override
    public CampusActivityStudent selectCampusActivityStudentByPayId(Long payId) {
        CampusActivityStudent campusActivityStudent = campusActivityStudentMapper.selectCampusActivityStudentByPayId(payId);
        XcOrders order = ordersService.getOrderByBusinessId(String.valueOf(payId));
        XcPayRecord xcPayRecord = new XcPayRecord();
        xcPayRecord.setOrderId(order.getId());
        List<XcPayRecord> xcPayRecords = payRecordService.selectXcPayRecordList(xcPayRecord);
        campusActivityStudent.setOrders(order);
        campusActivityStudent.setPayRecordList(xcPayRecords);
        return campusActivityStudent;
    }

    /**
     * 查询缴费名单列表
     *
     * @param queryActivityParamsDto 缴费名单
     * @return 缴费名单
     */
    @Override
    public List<CampusActivityStudent> selectCampusActivityStudentList(QueryActivityParamsDto queryActivityParamsDto) {
        return campusActivityStudentMapper.selectCampusActivityStudentList(queryActivityParamsDto);
    }


    /**
     * 查询缴费名单 - 订单 - 支付记录 列表
     *
     * @param queryActivityParamsDto 查询参数
     * @return 缴费名单集合
     */
    public List<CampusActivityStudent> selectActivityStudentOrderRecordList(QueryActivityStudentsRequestDto queryActivityParamsDto) {
        List<CampusActivityStudent> campusActivityStudents = campusActivityStudentMapper.selectActivityStudentOrderRecordList(queryActivityParamsDto);
        for (CampusActivityStudent acs : campusActivityStudents) {
            SysDept school = acs.getStudents().getSchool();
            acs.setDept(school);
        }
        return campusActivityStudents;
    }

    /**
     * 新增缴费名单
     *
     * @param campusActivityStudent 缴费名单
     * @return 结果
     */
    @Override
    public int insertCampusActivityStudent(CampusActivityStudent campusActivityStudent) {
        return campusActivityStudentMapper.insertCampusActivityStudent(campusActivityStudent);
    }

    /**
     * 新增用户角色信息
     *
     * @param actId      活动ID
     * @param studentIds 学生组
     */
    @Override
    public int batchActStudent(Long actId, Long[] studentIds) {
        campusActivityStudentMapper.deleteCampusActivityStudentByActId(actId);
        if (StringUtils.isNotEmpty(studentIds)) {
            List<CampusActivityStudent> list = new ArrayList<>(studentIds.length);
            for (Long stuId : studentIds) {
                CampusActivityStudent as = new CampusActivityStudent();
                as.setActId(String.valueOf(actId));
                as.setStudentId(String.valueOf(stuId));
                list.add(as);
            }
            return campusActivityStudentMapper.batchActStudent(list);
        }
        return 0;
    }

    /**
     * 修改缴费名单
     *
     * @param campusActivityStudent 缴费名单
     * @return 结果
     */
    @Override
    public int updateCampusActivityStudent(CampusActivityStudent campusActivityStudent) {
        return campusActivityStudentMapper.updateCampusActivityStudent(campusActivityStudent);
    }

    /**
     * 批量删除缴费名单
     *
     * @param payIds 需要删除的缴费名单主键
     * @return 结果
     */
    @Override
    public int deleteCampusActivityStudentByPayIds(Long[] payIds) {
        return campusActivityStudentMapper.deleteCampusActivityStudentByPayIds(payIds);
    }

    /**
     * 删除缴费名单信息
     *
     * @param payId 缴费名单主键
     * @return 结果
     */
    @Override
    public int deleteCampusActivityStudentByPayId(Long payId) {
        return campusActivityStudentMapper.deleteCampusActivityStudentByPayId(payId);
    }
}
